<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/layout :: head}">
    <title>认证服务</title>
</head>
<body>
    <div class="container">
        <h2>登录</h2>
        
        <!-- 消息提示 -->
        <div th:if="${error}" class="alert error">
            <span th:text="${error}">登录失败</span>
        </div>
        
        <div th:if="${success}" class="alert success">
            <span th:text="${success}">已退出</span>
        </div>

        <!-- OAuth2提示 -->
        <div id="oauth2-notice" class="oauth2-notice" style="display: none;">
            <p>OAuth2模式</p>
        </div>

        <!-- 登录表单 -->
        <form th:action="@{/login}" method="post" class="login-form">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required autofocus>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>

            <div class="form-group">
                <label>
                    <input type="checkbox" name="remember-me"> 记住我
                </label>
            </div>

            <button type="submit" class="btn-primary">登录</button>
        </form>
    </div>

    <script>
        const OAuth2Manager = {
            captureOAuth2Params: function() {
                const urlParams = new URLSearchParams(window.location.search);
                const params = {};
                
                if (urlParams.has('client_id')) {
                    params.client_id = urlParams.get('client_id');
                    params.client_secret = urlParams.get('client_secret');
                    params.redirect_uri = urlParams.get('redirect_uri');
                    params.scope = urlParams.get('scope');
                    params.state = 'random';
                    params.response_type = urlParams.get('response_type') || 'code';
                    
                    localStorage.setItem('oauth2_params', JSON.stringify(params));
                    
                    const cleanUrl = window.location.protocol + "//" + window.location.host + window.location.pathname;
                    window.history.replaceState({}, document.title, cleanUrl);
                    
                    return params;
                }
                
                return null;
            },
            
            hasOAuth2Params: function() {
                const stored = localStorage.getItem('oauth2_params');
                return stored !== null;
            }
        };
        
        document.addEventListener('DOMContentLoaded', function() {
            const oauth2Params = OAuth2Manager.captureOAuth2Params();
            
            if (oauth2Params || OAuth2Manager.hasOAuth2Params()) {
                document.getElementById('oauth2-notice').style.display = 'block';
            }
        });
        
        window.OAuth2Utils = {
            storeOAuth2Params: function(params) {
                localStorage.setItem('oauth2_params', JSON.stringify(params));
            },
            
            getOAuth2Params: function() {
                const stored = localStorage.getItem('oauth2_params');
                return stored ? JSON.parse(stored) : null;
            },
            
            clearOAuth2Params: function() {
                localStorage.removeItem('oauth2_params');
            },
            
            buildLoginUrl: function(params) {
                const loginUrl = new URL('/login', window.location.origin);
                
                if (params.client_id) {
                    loginUrl.searchParams.set('client_id', params.client_id);
                }
                if (params.client_secret) {
                    loginUrl.searchParams.set('client_secret', params.client_secret);
                }
                if (params.redirect_uri) {
                    loginUrl.searchParams.set('redirect_uri', params.redirect_uri);
                }
                if (params.scope) {
                    loginUrl.searchParams.set('scope', params.scope);
                }
                loginUrl.searchParams.set('state', 'random');

                if (params.response_type) {
                    loginUrl.searchParams.set('response_type', params.response_type);
                }
                
                return loginUrl.toString();
            }
        };
    </script>

    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 400px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        
        .container {
            background: white;
            padding: 20px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
            font-size: 18px;
        }
        
        .alert {
            padding: 8px;
            margin: 10px 0;
            border-radius: 4px;
            text-align: center;
            font-size: 14px;
        }
        
        .alert.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .alert.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .oauth2-notice {
            background: #e7f3ff;
            padding: 10px;
            border-radius: 4px;
            margin: 15px 0;
            border-left: 4px solid #007bff;
        }
        
        .oauth2-notice p {
            margin: 0;
            color: #004085;
            font-size: 14px;
        }
        
        .login-form {
            margin: 15px 0;
        }
        
        .form-group {
            margin-bottom: 15px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #333;
            font-size: 14px;
        }
        
        .form-group input[type="text"],
        .form-group input[type="password"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        
        .form-group input[type="text"]:focus,
        .form-group input[type="password"]:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 3px rgba(0,123,255,0.3);
        }
        
        .btn-primary {
            width: 100%;
            padding: 10px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 14px;
            cursor: pointer;
        }
        
        .btn-primary:hover {
            background: #0056b3;
        }
    </style>
</body>
</html> 